// hyb_yl/addPrescription/addPrescription.js
var app = getApp()
let util = require("../../utils/util");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0,
    ypList: []
  },
  textNum: function(e) {
    console.log(e.detail.value)
    this.setData({
      num: e.detail.cursor,
      textarr: e.detail.value
    })
  },
  cfmoney: function(e) {
    var money = e.detail.cursor
    this.setData({
      money: money
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var useropenid = options.useropenid
    var j_id = options.j_id
    var zid = options.zid
    that.setData({
      useropenid: useropenid,
      j_id: j_id,
      zid: zid
    })
    if (options.c_id) {
      app.util.request({
        'url': 'entry/wxapp/Goods.chufangdt',
        data: {
          c_id: options.c_id
        },
        success: function(res) {
          console.log(res)
          that.setData({
            c_id: options.c_id,
            ypList: res.data.ypList,
            textarr: res.data.textarr,
            uplogo: res.data.cfpic,
            money: res.data.money
          })
        }
      })
    }
    app.util.request({
      'url': 'entry/wxapp/Hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    var ypList = that.data.ypList
    if (currPage.data.sid) {
      var data = {
        sid: currPage.data.sid,
        sname: currPage.data.sname,
        sthumb: currPage.data.sthumb
      }
      ypList.push(data)
      var hash = [];
      ypList = ypList.reduce(function(x, y) {
        hash[y['sid']] ? '' : hash[y['sid']] = true && x.push(y);
        return x;
      }, []);
      console.log(ypList)
      that.setData({
        ypList: ypList
      })
    }
  },
  // 删除药品
  remove: function(e) {
    var index = e.currentTarget.dataset.index
    var ypList = this.data.ypList
    ypList.splice(index, 1)
    this.setData({
      ypList: ypList
    })
  },
  remove2: function(e) {
    this.setData({
      uplogo: ""
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  navyaopin(e) {
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/shopType/shopType?type=1',
    })
  },
  navyaopi2(e) {
    var that = this;
    var uniacid = app.siteInfo.uniacid;
    wx.chooseImage({
      count: 2, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        var tempFilePaths = res.tempFilePaths[0];
        console.log(tempFilePaths);
        wx.uploadFile({
          url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
          filePath: tempFilePaths,
          name: 'upfile',
          formData: {},
          success: function(res) {
            console.log(res.data)
            that.setData({
              uplogo: res.data
            })
          },
          fail: function(res) {
            console.log(res)
          },
        })
      }
    })
  },
  formSubmit() {
    var that = this
    var textarr = that.data.textarr
    var ypList = that.data.ypList
    var useropenid = that.data.useropenid
    var j_id = that.data.j_id
    var zid = that.data.zid
    var TIME = util.formatTime(new Date());
    app.util.request({
      'url': 'entry/wxapp/Goods.chufang',
      data: {
        textarr: textarr,
        ypList: ypList,
        useropenid: useropenid,
        j_id: j_id,
        zid: zid,
        cfpic: that.data.uplogo,
        money: that.data.money
      },
      success: function(res) {
        console.log(res)
        var msg = res.data
        var data = {
          "username": msg.username,
          'text': msg.text,
          "title": msg.title,
          "style": msg.style,
          'type': msg.type,
          'url': msg.url,
          'sate': msg.sate,
          'time': msg.time,
        }
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
          chufang: data,
          typecf:1
        })
        wx.navigateBack({
          detail: 1
        })
      }
    })
  },
  //查看处方
  yulan(e) {
    var current = e.currentTarget.dataset.src; //获取data-src
    wx.previewImage({
      current: current,
      urls: [current]
    })
  }
})